﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using GrayParrot.Web.Controls;
using GrayParrot.Core.Filters;
using Test.DAL;


namespace FimetContoDeposito.BLL.ResultSets
{
    public class CustomersResultSet : ResultSet<Customer>
	{
        public CustomersResultSet(DTParameters param, IQueryable<Customer> result)
            : base(param, result)
        {
        }

        public override Expression<Func<Customer, bool>> ApplyGeneralFilter(string searchPattern)
        {
            var predicate = PredicateBuilder.False<Customer>();

            if (!string.IsNullOrWhiteSpace(searchPattern))
            {
                predicate = predicate.Or(im => im.ContactTitle != null && im.ContactTitle.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.ContactName != null && im.ContactName.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.CompanyName != null && im.CompanyName.ToString().Contains(searchPattern));
            }

            return predicate;
        }

        public override Expression<Func<Customer, bool>> ApplyGeneralColumnsFilter(List<string> columnSearch)
        {
            var predicate = PredicateBuilder.True<Customer>();

            string m_id = columnSearch[ColumnIndex];
            string m_title = columnSearch[ColumnIndex];
            string m_name = columnSearch[ColumnIndex];
            string m_company = columnSearch[ColumnIndex];


            if (!string.IsNullOrWhiteSpace(m_title))
            {
                predicate = predicate.And(im => im.ContactTitle.Contains(m_title));
            }

            if (!string.IsNullOrWhiteSpace(m_name))
            {
                predicate = predicate.And(im => im.ContactName.Contains(m_name));
            }

            if (!string.IsNullOrWhiteSpace(m_company))
            {
                predicate = predicate.And(im => im.CompanyName.Contains(m_company));
            }

            return predicate;
        }
	}
}
